package com.xiyu.service.repository.files.agreement;

import com.xiyu.service.model.files.agreement.FilesHousingAgreementFetcher;
import com.xiyu.service.model.unit.unit.UnitUnitFetcher;
import com.xiyu.service.vo.files.housingAgreement.HousingAgreementPageQueryInput;
import com.xiyu.service.model.files.agreement.FilesHousingAgreement;
import com.xiyu.service.model.files.agreement.FilesHousingAgreementTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

public interface FilesHousingAgreementRepository extends JRepository<FilesHousingAgreement, Long> {
    FilesHousingAgreementTable filesHousingAgreementTable = FilesHousingAgreementTable.$;

    default Page<FilesHousingAgreement> pageQuery(HousingAgreementPageQueryInput inputVO){
        return
            sql().createQuery(filesHousingAgreementTable)
                    .whereIf(inputVO.getExpireDate() != null, () -> filesHousingAgreementTable.expireDate().between(inputVO.getExpireDate()[0], inputVO.getExpireDate()[1]))
                    .whereIf(inputVO.getSigningDate() != null, () -> filesHousingAgreementTable.signingDate().between(inputVO.getSigningDate()[0], inputVO.getSigningDate()[1]))
                    .whereIf(inputVO.getUnitId() != null, filesHousingAgreementTable.unitId().eq(inputVO.getUnitId()))
                    .orderBy(filesHousingAgreementTable.id().desc())
                    .select(filesHousingAgreementTable.fetch(FilesHousingAgreementFetcher.$.allTableFields().unit(UnitUnitFetcher.$.allTableFields())))
                    .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());

    }


}